﻿using UnityEngine;
using Newtonsoft.Json;
using System.Collections.Generic;



using UnityEngine;
using System.Collections;

/**
 * 本地存储key
 * @Author yuanji
 */
public class NativeStorageKey
{
    /** 观看过影片 */
    public const string WATCHED_VEDIO = "WATCHED_VEDIO";

    /** 版本控制 */
    public const string VERSION = "VERSION";

    /** 游戏数据 */
    public const string GAME_DATA = "GAME_DATA";

    /**布阵英雄默认选定*/
    public const string Level_SelectedHero = "Level_SelectedHero";

    /** 挑战。。*/
    public const string Instance_SelectedHero = "Instance_SelectedHero";
    public const string Instance_SelectedHero_Male = "Instance_SelectedHero_Male";

    /** 天梯 */
    public const string Ladder_SelectedHero = "Ladder_SelectedHero";
    public const string Ladder_DefendHero = "Ladder_DefendHero";

    public const string EmBattle_SelectedHero = "EmBattle_SelectedHero";


    /** 上次登录的用户名 */
    public const string LOGIN_LAST_NAME = "Login_LastName";

    /** 上次登录的密码 */
    public const string LOGIN_LAST_PWD = "Login_LastPWD";

    /** 上次登录帐号是否是游客 */
    public const string LOGIN_LAST_ACC_IS_GUEST = "Login_LastAccIsGuest";



    /** 上次登录的服务器 */
    public const string LOGIN_LAST_SRV = "Login_LastSRV";


    /** 数据：版本号 */
    public const string RES_DATA_VERSION = "ResData_Version";

    /** 数据：功能解锁 */
    public const string RES_DATA_UNLOCK_FUNC = "ResData_UnlockFunc";

    /** 数据：天梯奖励 */
    public const string RES_DATA_LADDER_BONUS = "ResData_Ladder_bonus";

    /** 数据：充值物品 */
    public const string RES_DATA_CHARGE_ITEM = "ResData_ChargeItem";

    /** 数据：许愿商店 */
    public const string RES_DATA_WISH_SHOP = "ResData_WishShop";

    /** 数据：vip权限 */
    public const string RES_DATA_VIP = "ResData_Vip";

    /** 数据：vip效果 */
    public const string RES_DATA_VIP_BUFF = "ResData_VipBuff";

    /** 数据：称号 */
    public const string RES_DATA_PLAYER_TITLE = "ResData_PlayerTitle";

    /** 数据：系统参数 */
    public const string RES_DATA_SYS_PARAM = "ResData_SysParam";

    /** 数据：玩家升级经验需求 */
    public const string RES_DATA_PLAYER_UPGRADE_REQ = "ResData_PlayerUpgradeReq";

    /** 数据：英雄升级经验需求 */
    public const string RES_DATA_HERO_UPGRADE_REQ = "ResData_HeroUpgradeReq";

    /** 数据：公会升级经验需求 */
    public const string RES_DATA_UNION_UPGRADE_REQ = "ResData_UnionUpgradeReq";

    /** 数据：Coser */
    public const string RES_DATA_COSER = "ResData_Coser";

    /** 数据：技能 */
    public const string RES_DATA_SKILL_ZIP_ID_ARR = "ResData_Skill_Zip_Id_Arr";
    public const string RES_DATA_SKILL = "ResData_Skill";

    /** 数据：英雄 */
    public const string RES_DATA_HERO_ZIP_ID_ARR = "ResData_Hero_Zip_Id_Arr";
    public const string RES_DATA_HERO = "ResData_Hero";

    /** 数据：物品 */
    private const string _RES_DATA_ITEM = "ResData_Item_{0}";

    /** 数据：关卡 */
    public const string RES_DATA_LEVEL_ID_ARR = "ResData_Level_Id_Arr";
    public const string RES_DATA_LEVEL = "ResData_Level";

    /** 数据：任务 */
    public const string RES_DATA_QUEST = "ResData_Quest";

    /** 数据：合成公式 */
    public const string RES_DATA_COMBINE_FORMULAR = "ResData_CombineFormular";

    /** 已读公告邮件 */
    public const string RES_DATA_MAIL_READ = "ResData_MailRead";

    /** 魔塔布阵英雄默认选定 */
    public const string Tower_EmBattle_SelectedHero = "TowerSelectedHero";
    /** 宫殿布阵英雄默认选定 */
    public const string Temple_EmBattle_SelectedHero = "TempleSelectedHero";

    /**弹幕开关*/
    public const string BARRAGE_SWITCH = "barrageSwith";

    /**最近弹幕保存列表*/
    public const string BARRAGE_LIST = "BARRAGE_LIST";

    /** 音效 */
    public const string SOUND_EFFECT_SWITCH = "SOUND_EFFECT_SWITCH";
    /** 音乐 */
    public const string MUSIC_SWITCH = "MUSIC_SWITCH";
    /** 12点领取体力 */
    public const string RECEIVE_ENERGY_NOON_SWITCH = "RECEIVE_ENERGY_NOON_SWITCH";
    /** 18点领取体力 */
    public const string RECEIVE_ENERGY_AFFTERNOON_SWITCH = "RECEIVE_ENERGY_AFFTERNOON_SWITCH";

  

    /** 数据key,指定玩家
     * key key值
     * playerId 玩家Id
     */
    public static string PlayerNativeDataKey(string key, int playerId)
    {
        if (key == null || playerId == 0) return null;
        string s = playerId + "_" + key;
     
        return s;

    }
}




/**@author yuanji*/
public class NativeStorage
{

    #region general key

    public const string DownloadComplete = "DownloadComplete";

    #endregion

    private const string KEY_AND_PLAYER_ID = "{0}_{1}";

    public static void Put(string key, string value)
    {
        PlayerPrefs.SetString(key, TextEncrypt.EncryptString(value));
    }

    public static void Put(string key, int value)
    {
        PlayerPrefs.SetInt(key, value);
    }

    public static void Put(string key, float value)
    {
        PlayerPrefs.SetFloat(key, value);
    }

    public static int GetInt(string key)
    {
        return PlayerPrefs.GetInt(key);
    }

    public static string GetString(string key)
    {
        if (!PlayerPrefs.HasKey(key))
        {
            return null;
        }
        return TextEncrypt.DecryptString(PlayerPrefs.GetString(key));
    }

    public static float GetFloat(string key)
    {
        return PlayerPrefs.GetFloat(key);
    }

    public static void PutObject(string key, object value)
    {
        if (key == null || value == null) return;
        if (PlayerPrefs.HasKey(key))
        {
            PlayerPrefs.DeleteKey(key);
        }
        string s = TextEncrypt.EncryptString(JsonConvert.SerializeObject(value));
        PlayerPrefs.SetString(key, s);
    }
    public static T GetObject<T>(string key)
    {
        string s = PlayerPrefs.GetString(key);
        if (s == null) return default(T);
        return (T)JsonConvert.DeserializeObject<T>(TextEncrypt.DecryptString(s));
    }

    public static void PutObjectBytes(string key, byte[] bytes)
    {
        if (key == null || bytes == null) return;
        if (PlayerPrefs.HasKey(key))
        {
            PlayerPrefs.DeleteKey(key);
        }
        string s = System.Convert.ToBase64String(bytes);
        PlayerPrefs.SetString(key, s);
    }

    public static byte[] GetObjectBytes(string key)
    {
        string s = PlayerPrefs.GetString(key);
        if (s == null) return null;
        return System.Convert.FromBase64String(s);
    }

    //public static T GetBytesObject<T>(string key)
    //{
    //    string s = PlayerPrefs.GetString(key);
    //    if (s == null) return default(T);
    //    return BsonConvert.DeserializeObject<T>(System.Convert.FromBase64String(s));
    //}

    /** 根据玩家id存储本地信息 */
    public static void PutObjectById(string key, object value, int playerId)
    {
        if (key == null || playerId == null) return;

        PutObject(string.Format(KEY_AND_PLAYER_ID, key, playerId), value);
    }
    /** 根据玩家id取出本地存储信息 */
    public static T GetObjectById<T>(string key, int playerId)
    {
        if (key == null || playerId == null) return default(T);

        return GetObject<T>(string.Format(KEY_AND_PLAYER_ID, key, playerId));
    }

    /** 清除所有本次存储数据 */
    public static void DeleteAll()
    {
        PlayerPrefs.DeleteAll();
    }


    public static void DeleteUselessData()
    {
        int lastLoginServerID = NativeStorage.GetInt(NativeStorageKey.LOGIN_LAST_SRV);
        string lastUName = NativeStorage.GetString(NativeStorageKey.LOGIN_LAST_NAME);
        string lastPWD = NativeStorage.GetString(NativeStorageKey.LOGIN_LAST_PWD);
        int lastAccIsGuest = NativeStorage.GetInt(NativeStorageKey.LOGIN_LAST_ACC_IS_GUEST);
        int downloadComplete = NativeStorage.GetInt(NativeStorage.DownloadComplete); 
        NativeStorage.DeleteAll();

        NativeStorage.Put(NativeStorageKey.LOGIN_LAST_SRV, lastLoginServerID);
        if (!string.IsNullOrEmpty(lastUName)) NativeStorage.Put(NativeStorageKey.LOGIN_LAST_NAME, lastUName);
        if (!string.IsNullOrEmpty(lastPWD)) NativeStorage.Put(NativeStorageKey.LOGIN_LAST_PWD, lastPWD);
        NativeStorage.Put(NativeStorageKey.LOGIN_LAST_ACC_IS_GUEST, lastAccIsGuest);
        NativeStorage.Put(NativeStorage.DownloadComplete, downloadComplete);
    }
}
